<template>
	<div>
		<!-- 面包屑导航区域 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
		  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
		  <el-breadcrumb-item>用户管理</el-breadcrumb-item>
		  <el-breadcrumb-item>角色列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!--卡片视图区域-->
		<el-card>
			<!--添加角色区域-->
			<el-row>
				<el-col>
					<el-button type="primary">添加角色</el-button>
				</el-col>
			</el-row>
			<!--角色列表区域-->
			<el-table :data="roleslist" border stripe>
				<!--展开列-->
				<el-table-column type="expand">
					<template slot-scope="scope">
						<!--一级权限-->
						<el-row v-for="(item1, i1) in scope.row.children" :key="item1.id" :class="['bdbutton',i1 === 0 ? 'bdtop':'','vcenter']">
							<!--渲染一级权限-->
							<el-col :span="5">
							<el-tag closable @close="romoveRightById(scope.row, item1.id)">{{ item1.authName}}</el-tag>
							<!--小箭头图标-->
							<i class="el-icon-caret-right"></i>
							</el-col>
							<!--渲染二三级权限-->
							<el-col :span="19">
								<!--通过v-for循环二级和三级权限-->
								<el-row v-for="(item2, i2) in item1.children" :key="item2.id" :class="[i2 === 0 ? '':'bdtop','vcenter']">
									<el-col :span="6">
										<el-tag type="success" closable @close="romoveRightById(scope.row, item2.id)">{{item2.authName}}</el-tag>
										<i class="el-icon-caret-right"></i>
									</el-col>
									<!--三级权限-->
									<el-col :span="18">
										<el-tag type="warning" v-for="(item3) in item2.children" :key="item3.id" closable @close="romoveRightById(scope.row, item3.id)">{{ item3.authName }}</el-tag>
									</el-col>
								</el-row>
							</el-col>
						</el-row>
					</template>
				</el-table-column>
				<!--索引列-->
				<el-table-column type="index"></el-table-column>
				<el-table-column el-table="角色名称" prop="roleName"></el-table-column>
				<el-table-column el-table="角色描述" prop="roleDesc"></el-table-column>
				<el-table-column el-table="操作">
				<!--作用域插槽-->
				<template slot-scope="scope">
					<el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
					<el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
					<el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>
				</template></el-table-column>
				
			</el-table>
		</el-card>
		<!-- 分配权限对话框-->
		<el-dialog
		  title="分配权限"
		  :visible.sync="setSetRightlogVisible"
		  width="50%" @close="setRightDialogClosed">
		  <el-tree :data="rightlist" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="treeKeys" ref="treeRef"></el-tree>
		  <span slot="footer" class="dialog-footer">
		    <el-button @click="setSetRightlogVisible = false">取 消</el-button>
		    <el-button type="primary" @click="allotRight">确 定</el-button>
		  </span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data () {
			return {
				// 所有角色列表
				roleslist: [],
				// 控制分配权限对话框的显示与隐藏
				setSetRightlogVisible: false,
				// 所有权限列表
				rightlist: [],
				//树形结构属性绑定对象
				treeProps:{
					label:'authName',
					children:'children'
				},
				// 所有默认勾选的id数组
				treeKeys: [],
				//当前即将被分配权限的角色id
				roleId: ''
			}
		},
		created () {
			this.getRoleList()
		},
		methods: {
			// 获取所有角色列表
			async getRoleList() {
				const { data: res } = await this.$http.get('roles')
				if(res.meta.status !==200 ){
					return this.$message.error('获取角色列表失败')
				}
				this.roleslist = res.data
				//console.log(this.roleslist)
			},
			// 根据id删除应用权限
			async romoveRightById (role, rightId) {
				//提示用户是否删除
				 const confirmResult = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
				          confirmButtonText: '确定',
				          cancelButtonText: '取消',
				          type: 'warning'
				       }).catch(err => err)
					   //console.log(confirmResult)
					   if(confirmResult !== 'confirm'){
						   return this.$message.info('您取消了本次操作')
					   }
					   //console.log('确认删除')
					   // 发起请求删除对应角色的权限
					   const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
					   if(res.meta.status !==200){
						   return this.$message.error('删除角色权限失败!')
					   }
					   // this.getRoleList()
					   role.children =res.data
					   this.$message.success('删除成功')
			},
			// 展示分配权限对话框
			async showSetRightDialog(role) {
				this.roleId=role.id
				const {data: res} = await this.$http.get('rights/tree')
				if(res.meta.status !==200 ){
					return this.$message.error('获取权限列表失败!')
				}
				this.rightlist = res.data
				this.getLeafKeys(role, this.treeKeys)
				this.setSetRightlogVisible=true
			},
			//获取所有的三级权限id
			getLeafKeys (node, arr) {
				//先判断该节点是否为三级节点
				//只需要通过判断该节点是否包含children属性
				if(!node.children){
					return arr.push(node.id)
				}
				node.children.forEach(item => this.getLeafKeys(item,arr))
			},
			//监听分配权限对话框的关闭事件
			setRightDialogClosed() {
				this.treeKeys= []
			},
			//点击确定按钮分配权限
			async allotRight(){
				// 获取所有勾选的key
				const keys=[
					...this.$refs.treeRef.getCheckedKeys(),
					...this.$refs.treeRef.getHalfCheckedKeys(),
				]
				// console.log(keys)
				// 转化成以,分隔的字符串
				const idStr=keys.join(',')
				const {data: res} = await this.$http.post(`roles/${this.roleId}/rights`,{rids: idStr})
				if(res.meta.status !==200){
					return this.$message.error('分配权限失败!')
				}
				this.$message.success('分配权限成功')
				this.getRoleList()
				this.setRightDialogClosed=false
			}
		}
	}
</script>

<style lang="less" scoped>
.el-tag{
	margin: 7px;
}
.bdtop{
	border-top: 1px solid #EEEEEE;
}
.bdbutton{
	border-bottom: 1PX solid #EEEEEE;
}
.vcenter{
	display: flex;
	align-items: center;
}
</style>
